package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 主题数据传输对象
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "主题信息")
public class ThemeDTO {

    @NotNull(message = "主题ID不能为空")
    @Positive(message = "主题ID必须是正整数")
    @Schema(description = "主题唯一标识", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
    private Long id;

    @NotBlank(message = "主题名称不能为空")
    @Size(max = 100, message = "主题名称长度不能超过100个字符")
    @Schema(description = "主题名称", example = "spring-framework", requiredMode = Schema.RequiredMode.REQUIRED)
    private String name;

    @NotBlank(message = "主题标签不能为空")
    @Size(max = 50, message = "主题标签长度不能超过50个字符")
    @Schema(description = "主题显示标签", example = "Spring框架", requiredMode = Schema.RequiredMode.REQUIRED)
    private String label;

    @Size(max = 500, message = "主题描述长度不能超过500个字符")
    @Schema(description = "主题描述", example = "Java Spring框架相关教学资源")
    private String description;

    @Schema(description = "封面图片URL", example = "/themes/spring/cover.jpg")
    private String coverImage;

    @PositiveOrZero(message = "排序顺序不能为负数")
    @Schema(description = "排序顺序", example = "1")
    @Builder.Default
    private Integer sortOrder = 0;

    @Schema(description = "是否启用", example = "true")
    @Builder.Default
    private Boolean enabled = true;

    @Schema(description = "主题颜色", example = "#3498db")
    private String color;

    @Schema(description = "图标名称", example = "Folder")
    private String icon;

    @Schema(description = "资源数量", example = "25")
    private Integer resourceCount;

    @Schema(description = "创建时间")
    private String createdAt;

    @Schema(description = "更新时间")
    private String updatedAt;

    @Schema(description = "副主题列表")
    private List<SubThemeDTO> subThemes;

    /**
     * 获取完整的主题信息
     */
    @Schema(description = "完整主题信息", accessMode = Schema.AccessMode.READ_ONLY)
    public String getFullThemeInfo() {
        return label + " (" + name + ")";
    }

    /**
     * 判断是否有副主题
     */
    @Schema(description = "是否有副主题", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean hasSubThemes() {
        return subThemes != null && !subThemes.isEmpty();
    }

    /**
     * 获取副主题数量
     ères     */
    @Schema(description = "副主题数量", accessMode = Schema.AccessMode.READ_ONLY)
    public int getSubThemeCount() {
        return hasSubThemes() ? subThemes.size() : 0;
    }

    /**
     * 验证主题是否有效
     */
    @Schema(description = "主题是否有效", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isValid() {
        return id != null && name != null && !name.trim().isEmpty() && label != null && !label.trim().isEmpty();
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Schema(description = "副主题信息")
    public static class SubThemeDTO {

        @NotNull(message = "副主题ID不能为空")
        @Positive(message = "副主题ID必须是正整数")
        @Schema(description = "副主题唯一标识", example = "101", requiredMode = Schema.RequiredMode.REQUIRED)
        private Long id;

        @NotBlank(message = "副主题名称不能为空")
        @Size(max = 100, message = "副主题名称长度不能超过100个字符")
        @Schema(description = "副主题名称", example = "spring-boot", requiredMode = Schema.RequiredMode.REQUIRED)
        private String name;

        @NotBlank(message = "副主题标签不能为空")
        @Size(max = 50, message = "副主題标签长度不能超过50个字符")
        @Schema(description = "副主题显示标签", example = "Spring Boot", requiredMode = Schema.RequiredMode.REQUIRED)
        private String label;

        @Size(max = 500, message = "副主题描述长度不能超过500个字符")
        @Schema(description = "副主题描述", example = "Spring Boot快速开发框架")
        private String description;

        @Schema(description = "封面图片URL", example = "/themes/spring/boot-cover.jpg")
        private String coverImage;

        @PositiveOrZero(message = "排序顺序不能为负数")
        @Schema(description = "排序顺序", example = "1")
        @Builder.Default
        private Integer sortOrder = 0;

        @Schema(description = "是否启用", example = "true")
        @Builder.Default
        private Boolean enabled = true;

        @Schema(description = "父主题ID", example = "1")
        private Long parentThemeId;

        @Schema(description = "资源数量", example = "15")
        private Integer resourceCount;

        /**
         * 获取完整的副主题信息
         */
        @Schema(description = "完整副主题信息", accessMode = Schema.AccessMode.READ_ONLY)
        public String getFullSubThemeInfo() {
            return label + " (" + name + ")";
        }

        /**
         * 验证副主题是否有效
         */
        @Schema(description = "副主题是否有效", accessMode = Schema.AccessMode.READ_ONLY)
        public boolean isValid() {
            return id != null && name != null && !name.trim().isEmpty() && label != null && !label.trim().isEmpty();
        }

        /**
         * 判断是否有父主题
         */
        @Schema(description = "是否有父主题", accessMode = Schema.AccessMode.READ_ONLY)
        public boolean hasParentTheme() {
            return parentThemeId != null;
        }
    }
}
